﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Net;//Endpoint
using System.Net.Sockets;//包含套接字
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace WPFExcelReport
{

    public partial class TPLCTCP
    {
        #region ==TIMES===========
        public static readonly DependencyProperty TIMESProperty = DependencyProperty.Register("TIMES",
            typeof(int), typeof(TPLCTCP),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTIMESChanged));
        public int TIMES
        {
            get
            {
                return (int)GetValue(TIMESProperty);
            }
            set
            {
                SetValue(TIMESProperty, value); 
            }
        }
        private static void OnTIMESChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPLCTCP;
            if (src != null)
            {
                src.TIMES = (int)e.NewValue;
            }
        }
        #endregion
        ZLANServer _Server;
        public ZLANServer Server
        {
            get
            {
                if(_Server==null)
                {
                    _Server = new ZLANServer(25003);
                }
                return _Server;
            }
        }
        Dictionary<string,ZLANClient> Clients
        {
            get
            {
                return Server.Clients;
            }
        }
        ZLANClient zLAN = null;
        bool isWatch = true;
        public TPLCTCP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_ZLAN();            
            this.DataContext = MV;
            InitializeComponent();
            TIMES = 4056;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            ZLANSVRSTATUS = "ToCheck";
        } 

        //开启监听 按钮
        private void btnStartListen_Click(object sender, RoutedEventArgs e)
        {
            Server.StartListen();
        }
        private void btnStopListen_Click(object sender, RoutedEventArgs e)
        {
            Server.ZLStopListen();
         }

        public override void OnEXAUFChg(string vlaue)
        {
            if (EXAUF.Length < 3)
            {
                return;
            }
            if (!Clients.ContainsKey(EXAUF))
                return;
            zLAN = Clients[EXAUF];
            base.OnEXAUFChg(vlaue);
        }

        public void ShowMsg(string msgStr)
        {
            this.Dispatcher.BeginInvoke(
                    new Action(() => { INFO += msgStr + Environment.NewLine; }),
                    DispatcherPriority.Input
                );
        } 

        private void ServerStart(object sender, RoutedEventArgs e)
        {
            //TMV.ZLANSTART();
        }
        private void ServerStop(object sender, RoutedEventArgs e)
        {
            //TMV.ZLANSTOP();
        }
        private void ServerReset(object sender, RoutedEventArgs e)
        {
            //TMV.ZLANRESET();
        }
        private void ServerStatusChk(object sender, RoutedEventArgs e)
        {
          // SResult= TMV.ZLANCHK();
            foreach(DataRow row in SResult.Tables[0].Rows)
            {
                ZLANSVRSTATUS = row["MESSAGE"].ToString();
            }
        }
        private void ServerWrite(object sender, RoutedEventArgs e)
        {
            TMV.or1 = TMV.OracleDB.CreateCommand();
            TMV.or1.CommandText = "SELECT ABEMANDANT||ABEAUFKRS||ABEAUFLFD||ABEAUFPOS||ABEAFOLGE||ABEAEVOR|| ABEMODNR || ABEEINSTNR AS BDEKEY, E.AMEBDEKEN,AMEBEZ AS BDEDES, P.*FROM ABARTE P"
                + " JOIN AMEINST E ON ABEMANDANT = E.AMEMANDANT AND E.AMEMODNR = ABEMODNR AND E.AMEEINSTNR = ABEEINSTNR JOIN ABFOLGE ON ABEMANDANT = ABFMANDANT AND ABEAUFKRS = ABFAUFKRS AND ABEAUFLFD = ABFAUFLFD AND ABEAUFPOS = ABFAUFPOS AND ABEAFOLGE = ABFAFOLGE JOIN AMSTAMM M ON M.AMMANDANT = ABFMANDANT AND M.AMKSTKRS = ABFKSTKRS AND AMKST = ABFKST AND M.AMTYP = ABFMTYP AND M.AMSTDVAR = 'J' WHERE E.AMENRKRS || E.AMELFDNR || E.AMEPOS = M.AMNRKRS || M.AMLFDNR || M.AMPOS AND "
                + " P.ABEMANDANT = '00'  AND P.ABEAUFKRS = 'JH' AND P.ABEAUFLFD = '000456' AND P.ABEAUFPOS = '004' AND P.ABEAFOLGE = '010' ORDER BY 1";
            SResult = TMV.Connect2Proxy(TMV.or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            { 
                PM["CHANNEL"] = row["AMEBDEKEN"].ToString();
                PM["VALUE"] = row["ABEWERTX"].ToString();//目标值文字
                TMV.ABP.SETZLPLC(PM); 
            }
        }

        private void ServerRead(object sender, RoutedEventArgs e)
        {
            TMV.or1 = TMV.OracleDB.CreateCommand();
            TMV.or1.CommandText = "SELECT ABEMANDANT||ABEAUFKRS||ABEAUFLFD||ABEAUFPOS||ABEAFOLGE||ABEAEVOR|| ABEMODNR || ABEEINSTNR AS BDEKEY,AMEINTERVAL, E.AMEBDEKEN,AMEBEZ AS BDEDES, P.*FROM ABARTE P"
                + " JOIN AMEINST E ON ABEMANDANT = E.AMEMANDANT AND E.AMEMODNR = ABEMODNR AND E.AMEEINSTNR = ABEEINSTNR JOIN ABFOLGE ON ABEMANDANT = ABFMANDANT AND ABEAUFKRS = ABFAUFKRS AND ABEAUFLFD = ABFAUFLFD AND ABEAUFPOS = ABFAUFPOS AND ABEAFOLGE = ABFAFOLGE JOIN AMSTAMM M ON M.AMMANDANT = ABFMANDANT AND M.AMKSTKRS = ABFKSTKRS AND AMKST = ABFKST AND M.AMTYP = ABFMTYP AND M.AMSTDVAR = 'J' WHERE E.AMENRKRS || E.AMELFDNR || E.AMEPOS = M.AMNRKRS || M.AMLFDNR || M.AMPOS AND "
                + " P.ABEMANDANT = '00'  AND P.ABEAUFKRS = 'JH' AND P.ABEAUFLFD = '000456' AND P.ABEAUFPOS = '004' AND P.ABEAFOLGE = '010' ORDER BY 1";
            SResult = TMV.Connect2Proxy(TMV.or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                PM["INTERVAL"] =(Convert.ToDouble( row["AMEINTERVAL"].ToString())/1000).ToString();
                PM["MSETID"] = row["BDEKEY"].ToString();
                PM["CHANNEL"] = row["AMEBDEKEN"].ToString();
                PM["VALUE"] = row["ABEWERTX"].ToString();//目标值文字
                TMV.ABP.READAM(PM);
            }
            
        }
        private void ServerStopRead(object sender, RoutedEventArgs e)
        {
            TMV.or1 = TMV.OracleDB.CreateCommand();
            TMV.or1.CommandText = "SELECT ABEAEVOR|| ABEMODNR || ABEEINSTNR AS BDEKEY,AMEINTERVAL, E.AMEBDEKEN,AMEBEZ AS BDEDES, P.*FROM ABARTE P"
                + " JOIN AMEINST E ON ABEMANDANT = E.AMEMANDANT AND E.AMEMODNR = ABEMODNR AND E.AMEEINSTNR = ABEEINSTNR JOIN ABFOLGE ON ABEMANDANT = ABFMANDANT AND ABEAUFKRS = ABFAUFKRS AND ABEAUFLFD = ABFAUFLFD AND ABEAUFPOS = ABFAUFPOS AND ABEAFOLGE = ABFAFOLGE JOIN AMSTAMM M ON M.AMMANDANT = ABFMANDANT AND M.AMKSTKRS = ABFKSTKRS AND AMKST = ABFKST AND M.AMTYP = ABFMTYP AND M.AMSTDVAR = 'J' WHERE E.AMENRKRS || E.AMELFDNR || E.AMEPOS = M.AMNRKRS || M.AMLFDNR || M.AMPOS AND "
                + " P.ABEMANDANT = '00'  AND P.ABEAUFKRS = 'JH' AND P.ABEAUFLFD = '000456' AND P.ABEAUFPOS = '004' AND P.ABEAFOLGE = '010' ORDER BY 1";
            SResult = TMV.Connect2Proxy(TMV.or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                PM["INTERVAL"] = (Convert.ToDouble(row["AMEINTERVAL"].ToString()) / 1000).ToString();
                PM["MSETID"] = row["BDEKEY"].ToString();
                PM["CHANNEL"] = row["AMEBDEKEN"].ToString();
                PM["VALUE"] = row["ABEWERTX"].ToString();//目标值文字
                TMV.ABP.STOPAM(PM);
            } 
        }

        private void ClientsAllReading(object sender, RoutedEventArgs e)
        {
            TMV.or1 = TMV.OracleDB.CreateCommand();
            TMV.or1.CommandText = "SELECT E.*FROM AMSTAMM JOIN AMEINST E ON AMMANDANT = AMEMANDANT AND AMNRKRS = AMENRKRS AND AMLFDNR = AMELFDNR AND AMPOS = AMEPOS"
                + " WHERE AMART<> 'ZLAN    ' AND AMEBDEKEN<> '        '";
            SResult = TMV.Connect2Proxy(TMV.or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                PM["INTERVAL"] = (Convert.ToDouble(row["AMEINTERVAL"].ToString()) / 1000).ToString();
                PM["MSETID"] = "DUMMY";
                PM["CHANNEL"] = row["AMEBDEKEN"].ToString();
                PM["VALUE"] = "-1";//目标值文字
                for(int i=0;i<TIMES;i++)
                {
                    TMV.ABP.READAM(PM);
                }
                
            }
        } 
     
    }
}
